package org.example;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Timer;
import java.util.TimerTask;

// 第一个文字滚动状态
public abstract class TextScrollState1 implements GameState {
    private final CodeRebirthGame game;
    private String fullText = "这是一个重要的系统消息...请仔细阅读...";
    private String displayedText = "";
    private int charIndex = 0;
    private Timer textTimer;

    public TextScrollState1(CodeRebirthGame game) {
        this.game = game;
    }

    @Override
    public void enter() {
        game.setLayout(new BorderLayout());
        game.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                game.changeState(new ImageState2(game));
            }
        });

        // 文字逐个显示效果
        textTimer = new Timer();
        textTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if (charIndex < fullText.length()) {
                    displayedText += fullText.charAt(charIndex);
                    charIndex++;
                    game.repaint();
                } else {
                    this.cancel();
                }
            }
        }, 0, 100);
    }

    @Override
    public void render(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, game.getWidth(), game.getHeight());

        // 半透明蓝色字幕板
        Graphics2D g2d = (Graphics2D)g;
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f));
        g2d.setColor(Color.BLUE);
        int panelWidth = 600;
        int panelHeight = 200;
        g2d.fillRect((game.getWidth()-panelWidth)/2, (game.getHeight()-panelHeight)/2, panelWidth, panelHeight);

        // 白色文字
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
        g2d.setColor(Color.WHITE);
        g2d.setFont(game.getResourceManager().getFont("title", 24f));

        // 文字换行处理
        FontMetrics fm = g2d.getFontMetrics();
        int lineHeight = fm.getHeight();
        int x = (game.getWidth()-panelWidth)/2 + 20;
        int y = (game.getHeight()-panelHeight)/2 + 40;

        String[] words = displayedText.split(" ");
        String line = "";
        for (String word : words) {
            if (fm.stringWidth(line + word) < panelWidth - 40) {
                line += word + " ";
            } else {
                g2d.drawString(line, x, y);
                y += lineHeight;
                line = word + " ";
            }
        }
        g2d.drawString(line, x, y);
    }

    @Override public void update() {}

    @Override
    public void exit() {
        if (textTimer != null) {
            textTimer.cancel();
        }
        game.getContentPane().removeAll();
    }

    @Override public void handleInput(KeyEvent e) {}

    public abstract void render();
}
